define([
  'text!' + ELMP.resource('components/selectFile/index.html', 'lib')
],
  function (template) {
    return {
      template,
      name: 'SelectFile',
      props: {
        title: { type: String, default: '关联文件' },
        projectId: { type: String, required: true }
      },
      data() {
        return {
          dialogVisible: false,
          defaultProps: {
            children: 'children',
            label: 'title',
            isLeaf(data) {
              return data.flag === '0'
            }
          }
        }
      },
      methods: {
        save() {
          const node = this.$refs.tree.getCurrentNode()
          if(!node){
            this.dialogVisible = false
            return
          }
          if (node.flag === '1') {
            this.$message.warning('请选择文件')
            return
          }
          this.dialogVisible = false
          this.$emit('save', node)
        },
        open({ projectId }) {
          this.projectId = projectId
          this.getTreedata()
          this.dialogVisible = true
        },
        async loadNode(node, resolve) {
          if (node.level === 0) {
            const data = await this.getTreedata(this.projectId)
            return resolve(data);
          }
          const data = await this.getTreedata(node.data.id)
          return resolve(data)
        },
        async getTreedata(folderId) {
          const data = await $.elApi.doc.list({
            pageNo: 1,
            pageSize: 10000,
            folderId,
            contextId: this.projectId,
            orderByField: 'title',
            order: 'asc'
          })
          return data.data.records
        }
      }
    }
  })
